package com.example.elderlycare.controller;

import com.example.elderlycare.controller.entity.ComplaintRequestBody;
import com.example.elderlycare.dao.entity.Complaint;
import com.example.elderlycare.service.ComplaintService;
import com.example.elderlycare.service.entity.ComplaintListResponseBody;
import com.example.elderlycare.utils.Result;
import com.example.elderlycare.utils.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/complaint")
public class ComplaintController {

    @Resource
    ComplaintService complaintService;

    /**
     * 查询投诉记录
     *
     * @param elderName     老人姓名
     * @param accuser       投诉人
     * @param complainWayId 投诉方式id
     * @param startDate     投诉时间范围查询开始日期
     * @param endDate       投诉时间范围查询结束日期
     * @param pageSize      页大小
     * @param current       当前页
     * @return ComplaintListResponseBody
     */
    @GetMapping("/query")
    public Result<ComplaintListResponseBody> queryComplaints(
            @RequestParam(value = "elderName", required = false) String elderName,
            @RequestParam(value = "accuser", required = false) String accuser,
            @RequestParam(value = "complainWayId", required = false) String complainWayId,
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate,
            @RequestParam(defaultValue = "10", value = "pageSize") int pageSize,
            @RequestParam(defaultValue = "1", value = "current") int current
    ) {
        Map<String, Object> params = new HashMap<>();
        params.put("elderName", elderName != null ? "%" + elderName + "%" : null);
        params.put("accuser", accuser != null ? "%" + accuser + "%" : null);
        params.put("complainWayId", complainWayId);
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        params.put("limit", pageSize);
        params.put("offset", (current - 1) * pageSize);
        try {
            ComplaintListResponseBody res = complaintService.queryComplaints(params);
            return Result.success(res);
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }

    /**
     * 新增投诉记录
     *
     * @param req ComplaintRequestBody.addComplaint
     * @return null
     */
    @PostMapping("/add")
    public Result<?> addComplaint(@Valid @RequestBody ComplaintRequestBody.AddComplaint req) {
        Complaint complaint = new Complaint();
        BeanUtils.copyProperties(req, complaint);
        complaint.setTime(new Date());
        try {
            boolean flag = complaintService.save(complaint);
            if (flag) {
                return Result.success(null);
            } else {
                log.warn("add Complaint error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }

    /**
     * 修改投诉记录
     *
     * @param req ComplaintRequestBody.updateComplaint
     * @return null
     */
    @PutMapping("/update")
    public Result<?> updateComplaint(@Valid @RequestBody ComplaintRequestBody.UpdateComplaint req) {
        Complaint complaint = new Complaint();
        BeanUtils.copyProperties(req, complaint);
        try {
            boolean flag = complaintService.updateById(complaint);
            if (flag) {
                return Result.success(null);
            } else {
                log.warn("update Complaint error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }

    /**
     * 删除投诉记录
     *
     * @param id 记录id
     * @return null
     */
    @DeleteMapping("/delete")
    public Result<?> deleteEvaluation(@RequestParam("id") @NotEmpty Integer id) {
        try {
            boolean flag = complaintService.removeById(id);
            if (flag) {
                return Result.success(null);
            } else {
                log.warn("delete Complaint error");
                return Result.error(ResultEnum.DEFAULT_ERROR);
            }
        } catch (Exception e) {
            log.warn("Exception", e);
            return Result.error(ResultEnum.DEFAULT_ERROR);
        }
    }
}
